package com.shelpe.services.sinbad.controller.actionLog.impl.adgroup;

import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import com.shelpe.services.sinbad.controller.actionLog.ActionLogDto;
import com.shelpe.services.sinbad.controller.actionLog.ActionLogService;
import com.shelpe.services.sinbad.controller.actionLog.AdgroupVo;
import com.shelpe.services.sinbad.entity.jpa.Adgroup;
import com.shelpe.services.sinbad.entity.jpa.AdgroupLog;
import com.shelpe.services.sinbad.entity.jpa.Campaign;

@Service
public class AdgroupUpdateStatusConverter extends ActionLogService {
	@Autowired
	private MessageSource messageSource;

	@Override
	public ActionLogDto convertAdgroup(AdgroupLog entity, Map<Long, Campaign> campaignMap,
			Map<Long, Adgroup> adgroupMap) {
		boolean adgroupUpdateStatusConverter = entity.getAction() == AdgroupLog.Action.MANUAL_PAUSE
				|| entity.getAction() == AdgroupLog.Action.AUTO_PAUSE
				|| entity.getAction() == AdgroupLog.Action.AUTO_OPEN
				|| entity.getAction() == AdgroupLog.Action.AUTO_OPEN;
		if (adgroupUpdateStatusConverter) {
			ActionLogDto dto = new ActionLogDto();
			dto.from(entity);
			if (StringUtils.isNotBlank(entity.getNote())) {
				AdgroupVo vo = this.unserialize(entity.getNote());
				if (vo != null) {
					String status = "暂停";
					String statusMessage = "暂停推广";
					if ("online".equals(vo.getOnlineStatus())) {
						status = "开启";
						statusMessage = "推广中";
					}
					String[] params = { vo.getProductName(), status, statusMessage };
					dto.setReason(messageSource.getMessage("message.adgroup.status.reason", params, Locale.ENGLISH));
					return dto;
				}
			}
		}
		return null;
	}
}
